<?php defined('SYSPATH') or die('No direct script access.');

class Controller_CPanel_I18n extends Controller_CPanel {
    
    public function action_index() {
        
        //Actions
        
        if (isset($_POST['save'])) {
            $this->save();
        } elseif (isset($_POST['remove'])) {
            $this->remove();
        }
        
        //Render
        
        $view = View::factory('cpanel/content/i18n');
        $app_config = Kohana::$config->load('app');
        
        $view->languages = Arr::get($app_config, 'languages', array());
        
        $strings = array();
        foreach ($view->languages as $lang => $desk)
            $strings[$lang] = $this->load($lang);
        
        $view->strings = $strings;
        
        $this->title = 'i18n';
        $this->content = $view->render();
    }
    
    private function load($lang) {
        $filepath = APPPATH . 'i18n' . DIRECTORY_SEPARATOR . $lang . EXT;
        
        if (is_file($filepath))
            return include $filepath;
        
        return array();
    }
    
    private function save() {
        $lang = Arr::get($_POST, 'lang', '');
        $old_string = Arr::get($_POST, 'old_string', '');
        $string = Arr::get($_POST, 'string', '');
        $translation = Arr::get($_POST, 'translation', '');
        
        $strings = $this->load($lang);
        
        if ($old_string != '' && $old_string != $string)
            unset($strings[$old_string]);
        
        $strings[$string] = $translation;
        
        file_put_contents(APPPATH . 'i18n' . DIRECTORY_SEPARATOR . $lang . EXT,
                $this->encode($strings));
        
        $this->add_message('Saved!!!', 'success');
    }
    
    private function remove() {
        $lang = Arr::get($_POST, 'lang', '');
        $old_string = Arr::get($_POST, 'old_string', '');
        
        $strings = $this->load($lang);
        unset($strings[$old_string]);
        
        file_put_contents(APPPATH . 'i18n' . DIRECTORY_SEPARATOR . $lang . EXT,
                $this->encode($strings));
        
        $this->add_message('Removed!!!', 'success');
    }

    protected function encode($array, $pref = "\t") {
        if ($pref == "\t") {
            $res = "<?php defined('SYSPATH') or die('No direct script access.');\n\nreturn array(\n";
        } else {
            $res = "array(\n";
        }
        
        foreach ($array as $i => $v)
            $res .= $pref . (!is_numeric($i) ? "'" . str_replace("'", "\\'", $i) . "' => " : "") .
                    (is_array($v)
                        ? self::encode($v, $pref . "\t")
                        : (is_numeric($v)
                                ? $v 
                                : (is_bool($v)
                                    ? ($v ? 'true' : 'false')
                                    : "'" . preg_replace("/(?<!\\\)'/ui", "\\'", str_replace("\\'", "\\\\\\'", $v)) . "'"))) . ",\n";

        return $res . substr($pref, 0, -1) . ($pref == "\t" ? ");" : ")");
    }
    
}